/*
 * create by Littleken at 2019/3/19 17:35
 */
package com.littleken.sellproject.controller;

import com.littleken.sellproject.config.CookieConstant;
import com.littleken.sellproject.config.RedisConstant;
import com.littleken.sellproject.enums.ResultEnum;
import com.littleken.sellproject.model.SellerInfo;
import com.littleken.sellproject.service.SellerService;
import com.littleken.sellproject.utils.CookieUtil;
import com.littleken.sellproject.utils.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Controller
@RequestMapping("/seller")
@Slf4j
public class SellerUserController {

    @Resource
    private SellerService service;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 登录接口
     * @param openid 通过openid登录
     * @param response
     * @param map
     * @return
     */
    @GetMapping("/login")
    public ModelAndView login(@RequestParam(value = "openid",required=false) String openid
                                ,HttpServletResponse response
                                , Map<String, Object> map){
        if(openid ==null){
            return new ModelAndView("index/index");
        }
        SellerInfo seller = service.findSellerInfoByOpenid(openid);

        if(seller == null){
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url", "error");
            return new ModelAndView("common/error");
        }

        //2. 设置token至redis
        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPIRE;//key过期时间 2小时

        /**
         * key = token_976cf9ec-0b41-4504-a6a0-f699343fa173
         * value = openID
         * expire 过期时间
         * TimeUnit.SECONDS 时间格式 秒
         */
        redisTemplate.opsForValue().set( String.format(RedisConstant.TOKEN_PREFIX,token)
                    ,openid,expire, TimeUnit.SECONDS);

        //3. 设置token至cookie
        CookieUtil.set(response, CookieConstant.TOKEN, token, expire);

        map.put("msg", ResultEnum. SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");

        return new ModelAndView("common/success");
    }

    /**
     * 注销接口
     * @param request
     * @param response
     * @param map
     * @return
     */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                               HttpServletResponse response,
                               Map<String, Object> map) {
        //获取一个叫token的cookic值
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        //若能获取到
        if (cookie != null) {
            //2. 清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

            //3. 清除cookie
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        return new ModelAndView("index/index");
    }
}
